//
//  XuexiListViewModel.m
//  GuShiNews
//
//  Created by 丁一 on 2019/8/23.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "XuexiListViewModel.h"
#import "HJonesNetting.h"
#import "XueXiModel.h"
#import "HiJonesClass.h"

@implementation XuexiListViewModel

- (instancetype)init:(id)responseObject{
    if (self = [super init]) {
        
        [HJonesNetting JonesNetting_POST:responseObject
                                  andURL:Xuexi_Get_list
                              andtimeout:20.0
                                finished:^(NSError * _Nonnull error, NSDictionary * _Nonnull resultDict) {
                                    if (error) {
                                        [SVProgressHUD showErrorWithStatus:@"网络加载失败"];
                                        [self.delegate XuexiListSucces:0 array:nil page:[[responseObject objectForKey:@"page"] integerValue]];
                                    }else{
                                        if ([[resultDict objectForKey:@"Code"] longLongValue]==1000) {
                                            
                                            NSMutableArray *hudonglist = [XueXiModel mj_objectArrayWithKeyValuesArray:[resultDict objectForKey:@"HomeNewsArray"]];
                                            
                                            [hudonglist enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                                XueXiModel *model = (XueXiModel *)obj;
                                                
                                                model.title_height = [HuaHereClass size:model.x_tittle font:[UIFont boldSystemFontOfSize:23] width:RECRWITCH-50 height:1000].height+20;

                                                model.info_height =[HiJonesClass getContent:model.x_info width:RECRWITCH-50 font:[UIFont systemFontOfSize:17] jianju:3]+30;
                                                
                                                
                                                NSString *temp = [model.x_info stringByReplacingOccurrencesOfString:@" " withString:@""];
                                                temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
                                                temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                                                temp = [temp stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
                                                model.x_info_string = temp;
                                                
                                                NSString *lincoutn = [NSString stringWithFormat:@"%ld", (long)model.x_info_string.length];
                                                CGFloat lin_flaot;
                                                if (model.x_info_string.length>1000) {
                                                    lin_flaot = [lincoutn floatValue]/650.00f;
                                                }else if (model.x_info_string.length>2000){
                                                    lin_flaot = [lincoutn floatValue]/750.00f;
                                                }
                                                else{
                                                    lin_flaot = [lincoutn floatValue]/600.00f;
                                                }
                                                
                                                if (lin_flaot>1) {
                                                    model.x_time=[NSString stringWithFormat:@"阅读约%.0f分钟",lin_flaot];
                                                }else{
                                                    model.x_time=[NSString stringWithFormat:@"阅读约%.0f秒",lin_flaot*60];
                                                }
                                            }];
                                            
                                            [self.delegate XuexiListSucces:1 array:hudonglist page:[[resultDict objectForKey:@"Page"] intValue]];
                                        }else{
                                            [SVProgressHUD showErrorWithStatus:[resultDict objectForKey:@"Message"]];
                                            [self.delegate XuexiListSucces:0 array:nil page:[[responseObject objectForKey:@"page"] integerValue]];
                                        }
                                    }
                                }
                              passmanger:^(NSURLSessionDataTask * _Nonnull manger) {
                                  
                              }
                             andprogress:^(NSProgress * _Nonnull CProgress) {
                                 
                             }];
        
    }
    return self;
}

@end
